;$(function () {
    layui.use(['form', 'laydate', 'table', 'element'], function () {
        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            element = layui.element;
        var nowDate = getNowAndFormatDate();
        var now = {};
        now[nowDate] = "今天";
        let today = nowDate + " 00:00:00 - " + nowDate + " 23:59:59";
        laydate.render({
            elem: '#salesReportDate'
            , range: true
            , trigger: 'click'
            , type: 'datetime'
            , mark: now
            , value: today
            , done: function (value) {
                dateFun(value);
            }
        });

        $(".layui-btn.layui-btn-primary").on('click', function () {
            let event = $(this).attr("lay-event");
            switch (event) {
                case 'today':
                    $("#salesReportDate").val(today);
                    dateFun($("#salesReportDate").val());
                    break;
                case 'yesterday':
                    let yesterday = getYesterdayAndFormatDate();
                    $("#salesReportDate").val(yesterday + " 00:00:00 - " + yesterday + " 23:59:59");
                    dateFun($("#salesReportDate").val());
                    break;
                case 'lastWeek':
                    let lastWeek = getWeekAndFormatDate();
                    $("#salesReportDate").val(lastWeek + " 00:00:00 - " + nowDate + " 23:59:59");
                    dateFun($("#salesReportDate").val());
                    break;
                case 'lastMonth':
                    let lastMonth = getMouthAndFormatDate();
                    $("#salesReportDate").val(lastMonth + " 00:00:00 - " + nowDate + " 23:59:59");
                    dateFun($("#salesReportDate").val());
                    break;
                case 'lastYear':
                    let lastYear = getYearAndFormatDate();
                    $("#salesReportDate").val(lastYear + " 00:00:00 - " + nowDate + " 23:59:59");
                    dateFun($("#salesReportDate").val());
                    break;
            }
            form.render();
        });
    });
    var myChart = echarts.init(document.getElementById('salesReportDiv'));
    // 指定图表的配置项和数据
    let xdate = [];
    xdate.push()
    myChart.setOption({
        title: {
            text: '商品销售额统计'
        },
        tooltip: {},
        legend: {
            data: ['销售额']
        },
        xAxis: {
            data: xdate
        },
        yAxis: {},
        series: [{
            name: '销量',
            type: 'bar',
            data: []
        }]
    });

    function dateFun(val) {
// 显示标题，图例和空的坐标轴
        $.ajax({
            url: 'loadSalesReport',
            type: "post",
            dataType: "json",
            data: {
                "val": val,
            },
            async: false,
            success: function (result) {
                if (result.code === true) {
                    myChart.showLoading();
                    myChart.setOption({
                        xAxis: {
                            data: result.xAxis
                        },
                        series: [{
                            // 根据名字对应到相应的系列
                            name: '销量',
                            data: result.data
                        }]
                    });
                    myChart.hideLoading();
                } else {
                    layer.msg(result.msg);
                }
            },
        });

    }
})
;